package com.ruoyi.assets.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.assets.domain.StateCollection;
import com.ruoyi.assets.mapper.StateCollectionMapper;
import com.ruoyi.assets.service.IStateCollectionService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 收藏Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class StateCollectionServiceImpl extends ServiceImpl<StateCollectionMapper, StateCollection> implements IStateCollectionService {
    
    /**
     * 查询收藏列表
     * 
     * @param stateCollection 查询条件
     * @return 收藏列表
     */
    @Override
    public List<StateCollection> selectStateCollectionList(StateCollection stateCollection) {
        return baseMapper.selectStateCollectionList(stateCollection);
    }
    
    /**
     * 通过用户ID查询收藏列表
     * 
     * @param userId 用户ID
     * @return 收藏列表
     */
    @Override
    public List<StateCollection> selectCollectionsByUserId(Long userId) {
        return baseMapper.selectCollectionsByUserId(userId);
    }
    
    /**
     * 添加收藏
     * 
     * @param userId 用户ID
     * @param assetId 资产ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addCollection(Long userId, Long assetId) {
        // 检查是否已收藏
        LambdaQueryWrapper<StateCollection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StateCollection::getUserId, userId)
                   .eq(StateCollection::getAssetId, assetId)
                   .eq(StateCollection::isDeleted, false);
                   
        StateCollection collection = getOne(queryWrapper);
        
        if (collection != null) {
            // 已经收藏过，只更新时间
            collection.setUpdateTime(LocalDateTime.now());
            return updateById(collection);
        }
        
        // 添加收藏
        collection = new StateCollection();
        collection.setUserId(userId);
        collection.setAssetId(assetId);
        collection.setDeleted(false);
        
        return save(collection);
    }
    
    /**
     * 取消收藏
     * 
     * @param userId 用户ID
     * @param assetId 资产ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean cancelCollection(Long userId, Long assetId) {
        LambdaQueryWrapper<StateCollection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StateCollection::getUserId, userId)
                   .eq(StateCollection::getAssetId, assetId)
                   .eq(StateCollection::isDeleted, false);
                   
        StateCollection collection = getOne(queryWrapper);
        if (collection != null) {
            collection.setDeleted(true);
            return updateById(collection);
        }
        return false;
    }
    
    /**
     * 检查是否已收藏
     * 
     * @param userId 用户ID
     * @param assetId 资产ID
     * @return 是否已收藏
     */
    @Override
    public boolean isCollected(Long userId, Long assetId) {
        StateCollection collection = baseMapper.checkCollection(userId, assetId);
        return collection != null;
    }
} 